package com.jilefojing.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jilefojing.beans.data.web.response.BannerData;
import com.jilefojing.beans.data.web.response.RecommendData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.PlatformEnum;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.enhanced.EnhancedBanner;
import com.jilefojing.beans.pojos.enhanced.EnhancedEArticle;
import com.jilefojing.beans.pojos.enhanced.EnhancedEAudio;
import com.jilefojing.beans.pojos.enhanced.EnhancedEVideo;
import com.jilefojing.beans.pojos.enhanced.EnhancedRecommend;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.service.IBannerService;
import com.jilefojing.service.IEArticleService;
import com.jilefojing.service.IEAudioService;
import com.jilefojing.service.IEVideoService;
import com.jilefojing.service.IRecommendService;
import com.jilefojing.util.Detect;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

@Controller
@RequestMapping("index")
public class IndexWeb extends BaseWeb {
	@Autowired
	private IBannerService bannerService;
	@Autowired
	private IRecommendService recommendService;
	@Autowired
	private IEAudioService eAudioService;
	@Autowired
	private IEVideoService eVideoService;
	@Autowired
	private IEArticleService eArticleService;

	@RequestMapping("")
	public String index(HttpServletRequest request) {
		UserContext userContext = null;
		try {
			userContext = super.getUserContext();
		} catch (UserContextException e) {
			// 可忽略
		}

		PlatformEnum platform = super.isMobile() ? PlatformEnum.WAP : PlatformEnum.PC;
		Pagination<EnhancedBanner> enhancedBannerPagination = new Pagination<EnhancedBanner>(1, Integer.MAX_VALUE, false);
		enhancedBannerPagination = bannerService.getPaginationForActive(platform, enhancedBannerPagination, userContext);
		if (Detect.notEmpty(enhancedBannerPagination.getItems())) {
			List<BannerData> banners = CommonConvent.convent2BannerDatas(enhancedBannerPagination.getItems());
			request.setAttribute("banners", banners);
		}

		Pagination<EnhancedRecommend> enhancedRecommendArticlePagination = new Pagination<EnhancedRecommend>(1, 5, false);
		enhancedRecommendArticlePagination = recommendService.getPaginationForActive(platform, ItemTypeEnumForSource.ARTICLE, enhancedRecommendArticlePagination, userContext);
		if (Detect.notEmpty(enhancedRecommendArticlePagination.getItems())) {
			List<BaseEnhanced> enhancedItems = new ArrayList<BaseEnhanced>();
			for (EnhancedRecommend _EnhancedRecommend : enhancedRecommendArticlePagination.getItems()) {
				enhancedItems.add(_EnhancedRecommend.getEnhancedItem());
			}

			enhancedItems = (List) eArticleService.appendEnhancedDCategories((List) enhancedItems, userContext);

			// if (userContext != null) {
			// EVideoService.appendEnhancedExtrals2User(enhancedItems,
			// userContext);
			// }
			String firstArticleImg = null;
			String firstArticleContent = "";

			EnhancedEArticle firstEnhancedEArticle = (EnhancedEArticle) enhancedRecommendArticlePagination.getItems().get(0).getEnhancedItem();
			Document firstDoc = Jsoup.parse(firstEnhancedEArticle.getContent());
			Elements firstImgs = firstDoc.select("img[src]");
			if (firstImgs.size() > 0) {
				firstArticleImg = firstImgs.get(0).attr("src").trim();
				firstImgs.remove();
			}
			Elements pElements = firstDoc.select("p");
			for (int i = 0; i < pElements.size(); i++) {
				firstArticleContent += pElements.get(i).toString();
				if (firstArticleContent.length() > 499) {
					break;
				}
			}
			firstArticleContent += "...";

			List<RecommendData> articleRecommends = CommonConvent.convent2RecommendDatas(enhancedRecommendArticlePagination.getItems());

			request.setAttribute("firstArticleImg", firstArticleImg);
			request.setAttribute("firstArticleContent", firstArticleContent);
			request.setAttribute("articleRecommends", articleRecommends);
		}

		Pagination<EnhancedRecommend> enhancedRecommendVideoPagination = new Pagination<EnhancedRecommend>(1, 8, false);
		enhancedRecommendVideoPagination = recommendService.getPaginationForActive(platform, ItemTypeEnumForSource.VIDEO, enhancedRecommendVideoPagination, userContext);
		if (Detect.notEmpty(enhancedRecommendVideoPagination.getItems())) {
			List<BaseEnhanced> enhancedItems = new ArrayList<BaseEnhanced>();
			for (EnhancedRecommend _EnhancedRecommend : enhancedRecommendVideoPagination.getItems()) {
				enhancedItems.add(_EnhancedRecommend.getEnhancedItem());
			}
			enhancedItems = (List) eVideoService.appendEnhancedChildren((List) enhancedItems, userContext);
			// if (userContext != null) {
			// EVideoService.appendEnhancedExtrals2User(enhancedItems,
			// userContext);
			// }
			this.resetEnhancedStatistics2NotEntityEnhancedEVideo((List) enhancedItems);
			List<RecommendData> videoRecommends = CommonConvent.convent2RecommendDatas(enhancedRecommendVideoPagination.getItems());
			request.setAttribute("videoRecommends", videoRecommends);
		}

		Pagination<EnhancedRecommend> enhancedRecommendAudioPagination = new Pagination<EnhancedRecommend>(1, 8, false);
		enhancedRecommendAudioPagination = recommendService.getPaginationForActive(platform, ItemTypeEnumForSource.AUDIO, enhancedRecommendAudioPagination, userContext);
		if (Detect.notEmpty(enhancedRecommendAudioPagination.getItems())) {
			List<BaseEnhanced> enhancedItems = new ArrayList<BaseEnhanced>();
			for (EnhancedRecommend _EnhancedRecommend : enhancedRecommendAudioPagination.getItems()) {
				enhancedItems.add(_EnhancedRecommend.getEnhancedItem());
			}
			enhancedItems = (List) eAudioService.appendEnhancedChildren((List) enhancedItems, userContext);
			if (userContext != null) {
				enhancedItems = (List<BaseEnhanced>) eAudioService.appendEnhancedExtralsForUser(enhancedItems, userContext);
			}
			this.resetEnhancedStatistics2NotEntityEnhancedEAudio((List) enhancedItems);
			List<RecommendData> audioRecommends = CommonConvent.convent2RecommendDatas(enhancedRecommendAudioPagination.getItems());
			request.setAttribute("audioRecommends", audioRecommends);
		}

		return "web/index";
	}

	private void resetEnhancedStatistics2NotEntityEnhancedEVideo(List<EnhancedEVideo> enhancedEVideos) {
		for (EnhancedEVideo _EnhancedEVideo : enhancedEVideos) {
			if (!_EnhancedEVideo.isEntity() && Detect.notEmpty(_EnhancedEVideo.getEnhancedChildren())) {
				EnhancedStatistics enhancedStatistics = new EnhancedStatistics();
				enhancedStatistics.setItemType(ItemTypeEnumForSource.AUDIO);
				for (EnhancedEVideo _childEnhancedEVideo : _EnhancedEVideo.getEnhancedChildren()) {
					if (_childEnhancedEVideo.getEnhancedStatistics() != null) {
						enhancedStatistics.setCountComment(enhancedStatistics.getCountComment() + _childEnhancedEVideo.getEnhancedStatistics().getCountComment());
						enhancedStatistics.setCountWatch(enhancedStatistics.getCountWatch() + _childEnhancedEVideo.getEnhancedStatistics().getCountWatch());
						enhancedStatistics.setCountPraise(enhancedStatistics.getCountPraise() + _childEnhancedEVideo.getEnhancedStatistics().getCountPraise());
						enhancedStatistics.setCountFavorite(enhancedStatistics.getCountFavorite() + _childEnhancedEVideo.getEnhancedStatistics().getCountFavorite());
					}
				}
				_EnhancedEVideo.setEnhancedStatistics(enhancedStatistics);
			}
		}
	}

	private void resetEnhancedStatistics2NotEntityEnhancedEAudio(List<EnhancedEAudio> enhancedEAudios) {
		for (EnhancedEAudio _EnhancedEAudio : enhancedEAudios) {
			if (!_EnhancedEAudio.isEntity() && Detect.notEmpty(_EnhancedEAudio.getEnhancedChildren())) {
				EnhancedStatistics enhancedStatistics = new EnhancedStatistics();
				enhancedStatistics.setItemType(ItemTypeEnumForSource.AUDIO);
				for (EnhancedEAudio _childEnhancedEAudio : _EnhancedEAudio.getEnhancedChildren()) {
					if (_childEnhancedEAudio.getEnhancedStatistics() != null) {
						enhancedStatistics.setCountComment(enhancedStatistics.getCountComment() + _childEnhancedEAudio.getEnhancedStatistics().getCountComment());
						enhancedStatistics.setCountWatch(enhancedStatistics.getCountWatch() + _childEnhancedEAudio.getEnhancedStatistics().getCountWatch());
						enhancedStatistics.setCountPraise(enhancedStatistics.getCountPraise() + _childEnhancedEAudio.getEnhancedStatistics().getCountPraise());
						enhancedStatistics.setCountFavorite(enhancedStatistics.getCountFavorite() + _childEnhancedEAudio.getEnhancedStatistics().getCountFavorite());
					}
				}
				_EnhancedEAudio.setEnhancedStatistics(enhancedStatistics);
			}
		}
	}

}
